package pl.bristleback.scrumtable.actions;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.token.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.bristleback.scrumtable.services.WidgetService;
import pl.bristleback.scrumtable.utils.SendMessageSupport;
import pl.bristleback.scrumtable.vo.Widget;
import pl.bristleback.server.bristle.actions.AnnotatedRemoteAction;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.binding.Bind;
import pl.bristleback.server.bristle.binding.ObjectBinder;
import pl.bristleback.server.bristle.binding.Property;
import pl.bristleback.server.bristle.token.Tokenize;
import pl.bristleback.server.bristle.token.Tokenizer;
import pl.bristleback.server.bristle.utils.BristleTokenFactory;

/**
 * //@todo class description
 * <p/>
 * Created on: 24.04.11 11:45 <br/>
 *
 * @author Paweł Machowski
 */
@Controller
@AnnotatedRemoteAction(actionName = "resizeWidget")
public class ResizeWidgetAction implements RemoteAction {
  private static Logger log = Logger.getLogger(ResizeWidgetAction.class.getName());


  @Autowired
  private WidgetService widgetService;

  @Autowired
  private SendMessageSupport sender;

  @Bind(properties = {
    @Property(name = "id", required = true),
    @Property(name = "position.height", required = true),
    @Property(name = "position.width", required = true)
  })
  private ObjectBinder<Widget> binder;

  @Tokenize(parameters = {"id", "position.height", "position.width"}, deepSearch = true)
  private Tokenizer tokenHelper;


  @Override
  public void performAction(WebSocketConnector connector, Token token) {
    Widget widget = binder.bind(token);

    Token sendToken = BristleTokenFactory.createJsonToken("widgetResized", new JSONObject());
    tokenHelper.tokenize(sendToken, widget.getId(), widget.getPosition().getHeight(), widget.getPosition().getWidth());

    sender.broadcastNotLogged(sendToken);

    widgetService.resizeWidget(widget);

  }
}